package com.kotlin.myapplication.mvp.ui.start

import android.content.res.TypedArray
import android.os.Bundle
import android.view.View
import com.kotlin.myapplication.R
import com.kotlin.myapplication.base.BaseActivity
import com.kotlin.myapplication.mvp.widget.LeadPageView

/**
 *@lil
 *@创建时间 2019/10/30 17:12
 *@描述
 */
import kotlinx.android.synthetic.main.activity_lead_page.*
import android.content.Intent
import android.os.Parcelable
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.kotlin.myapplication.mananger.APPSPManager
import org.jetbrains.anko.backgroundResource


class LeadPageActivity : BaseActivity(), View.OnClickListener {


    private val images = intArrayOf(R.drawable.leading1, R.drawable.leading2, R.drawable.leading3)
    private var titlelist: MutableList<String> = ArrayList<String>()
    private var contentlist: MutableList<String> = ArrayList<String>()
    var pageViews: MutableList<View> = ArrayList<View>()
    private var imageViews = ArrayList<ImageView>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun initView() {
        setContentView(R.layout.activity_lead_page)

        initleadpage()
        initPageView()
        initViewPager()
        lead_page_skip!!.setOnClickListener(this)

    }

    /**
     * 初始化标题和内容
     */
    private fun initleadpage() {
        var array: TypedArray = resources.obtainTypedArray(R.array.lead_page_content)
        for (i in 0..array.length() - 1) {
            titlelist.add(array.getString(i))
        }
        array.recycle()
        val contentArray = resources.obtainTypedArray(
                R.array.lead_page_content)
        for (i in 0..contentArray.length() - 1) {
            contentlist.add(contentArray.getString(i))
        }
        contentArray.recycle()
    }

    /**
     * 初始化引导页
     */
    private fun initPageView() {

        for (i in 0..images.size - 1) {
            val page = LeadPageView(getApplicationContext())
            page.setLeadPageIconResource(images[i])
            page.setLeadPageSkipButtonVisible(false);//关闭跳转
            if (i == images.size - 1) {
                page.setLeadPageOperateButtonVisible(true, resources.getString(R.string.experience_immediately))
                page.setLeadPageOperateButtonOnClickListener {
                    APPSPManager.setleadPage(true)
                    startActivity(Intent(this@LeadPageActivity, MenuAcitivity::class.java))
                    finish()
                }
            }
            pageViews.add(page)
        }
        //添加了一个空布局 在最后跳转选择页面
        var nullView = layoutInflater.inflate(R.layout.nullmail, null)
        pageViews.add(nullView)
    }

    private fun initViewPager() {
        //只添加三个圆点
        for (i in 0..pageViews.count() - 2) {
            //小点间距
            var params: LinearLayout.LayoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            params.setMargins(resources.getDimensionPixelSize(R.dimen.custom_margin), 0
                    , resources.getDimensionPixelSize(R.dimen.custom_margin), 0)
            //小点
            var pagePoint = ImageView(this@LeadPageActivity)
            pagePoint.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)

            if (0 == i) {
                // 默认选中第一张图片
                pagePoint.backgroundResource = R.drawable.page_indicator_focused
            } else {
                pagePoint.backgroundResource = R.drawable.page_indicator_unfocused
            }
            imageViews.add(pagePoint)
            //加入小点
            viewGroup.addView(pagePoint, params)
        }

        // 给viewpager设置适配器
        viewpager_start.setAdapter(GuidePageAdapter())
        viewpager_start.setOnPageChangeListener(GuidePageChangeListener())
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.lead_page_skip -> {
                APPSPManager.setleadPage(true)
                startActivity(Intent(this@LeadPageActivity, MenuAcitivity::class.java))
                finish()
            }

        }
    }

    inner class GuidePageAdapter : PagerAdapter() {
        override fun isViewFromObject(arg0: View, arg1: Any): Boolean {
            return arg0 === arg1
        }

        override fun getCount(): Int {
            if (null == pageViews) {
                return 0;
            } else {
                return pageViews.count()
            }
        }

        override fun getItemPosition(`object`: Any): Int {
            return super.getItemPosition(`object`)
        }

        override fun destroyItem(container: View, position: Int, `object`: Any) {
//            super.destroyItem(container, position, `object`)
            (container as ViewPager).removeView(pageViews[position])
        }

        override fun instantiateItem(container: View, position: Int): Any {
            (container as ViewPager).addView(pageViews[position])
            return pageViews[position]
        }

        override fun saveState(): Parcelable? {
            return null
        }
    }

    inner class GuidePageChangeListener : ViewPager.OnPageChangeListener {

        override fun onPageScrollStateChanged(p0: Int) {
        }

        override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {
        }

        override fun onPageSelected(p0: Int) {
            if (p0 == pageViews.count() - 1) {
                APPSPManager.setleadPage(true)
                startActivity(Intent(this@LeadPageActivity, MenuAcitivity::class.java))
                finish()
                return
            }

            for (i in 0..imageViews.size - 1) {
                if (p0 == i) {
                    imageViews.get(i).backgroundResource = R.drawable.page_indicator_focused
                } else {
                    imageViews.get(i).backgroundResource = R.drawable.page_indicator_unfocused
                }

            }
        }
    }

}